package com.cll.learn.stage10.example2;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述信息:
 * 国王对象
 * @author CLL
 * @version 1.0
 * @date 2021/5/13 8:25
 */
public class King {

    /**
     * 国王拥有的矿工的人数，相当于背包容量
     */
    private int person;

    /**
     * 国王拥有的矿产信息
     */
    private List<GoldResource> goldResourceList;

    /**
     * 获取当前情况下国王选择近况资源的信息
     * @return  包含最优解，以及最优解对应的金矿资源信息
     */
    public Result selectGoldResourceResult(){
        if (0 >= person) {
            return new Result(0, new ArrayList<>());
        }
        if (null == goldResourceList || 0 == goldResourceList.size()) {
            return new Result(0, new ArrayList<>());
        }
        // 本轮选择的金矿资源
        List<GoldResource> result = new ArrayList<>();
        // 本轮最优解
        int resultCapacity = 0;
        // 当前可以选择的金矿资源
        List<GoldResource> currentGoldResource = new ArrayList<>();
        for (GoldResource goldResource : goldResourceList) {

            // 不可选情况
            if (goldResource.getPerson() > person) {
                Result preResult = new King(person, currentGoldResource).selectGoldResourceResult();
                resultCapacity = preResult.getCapacity();
                result = preResult.getSelectGoldResource();
            } else {
                // 可选情况下不选
                Result preResult = new King(person, currentGoldResource).selectGoldResourceResult();
                int notSelectCapacity = preResult.getCapacity();
                List<GoldResource> notSelectGoldResourceList = preResult.getSelectGoldResource();

                // 可选情况下选择
                Result currentResult = new King(person - goldResource.getPerson(), currentGoldResource).selectGoldResourceResult();
                int selectCapacity = goldResource.getCapacity() + currentResult.getCapacity();
                List<GoldResource> selectGoldResourceList = new ArrayList<>(currentResult.getSelectGoldResource());
                selectGoldResourceList.add(goldResource);

                resultCapacity = Math.max(notSelectCapacity, selectCapacity);

                if (selectCapacity == Math.max(notSelectCapacity, selectCapacity)) {
                    // 选择了
                    result = selectGoldResourceList;
                } else {
                    result = notSelectGoldResourceList;
                }
                // 将当前矿产添加到可以选择矿产列表
                currentGoldResource.add(goldResource);
            }
        }
        return new Result(resultCapacity, result);
    }

    public King(int person, List<GoldResource> goldResourceList) {
        this.person = person;
        this.goldResourceList = goldResourceList;
    }

    public int getPerson() {
        return person;
    }

    public void setPerson(int person) {
        this.person = person;
    }

    public List<GoldResource> getGoldResourceList() {
        return goldResourceList;
    }

    public void setGoldResourceList(List<GoldResource> goldResourceList) {
        this.goldResourceList = goldResourceList;
    }


    @Override
    public String toString() {
        return "King{" +
                "person=" + person +
                ", goldResourceList=" + goldResourceList.toString() +
                '}';
    }
}